﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DTC.Database;

namespace DTC.Popups
{
    /// <summary>
    /// Interaction logic for IssueFormPopup.xaml
    /// </summary>
    public partial class IssueFormPopup : Window
    {
        public IssueFormPopup()
        {
            InitializeComponent();
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            issueForm = new DTCIssueForm();
            issueForm.IsDiscret = true;
            MainGrid.DataContext = issueForm;
        }

        private DTCIssueForm issueForm;
        public DTCIssueForm IssueForm
        {
            get
            {
                return issueForm;
            }
            set
            {
                if (issueForm != value)
                {
                    issueForm = value;
                    MainGrid.DataContext = issueForm;
                }
            }
        }
        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (this.DialogResult == true)
            {
                IssueFormTextBox.GetBindingExpression(TextBox.TextProperty).UpdateSource();
                List<string> errList = new List<string>();
                string message = "";
                if (new DTC.Validations.FormValidate().FormHasError(errList, MainGrid))
                {
                    foreach (string str in errList)
                        //if(!(message.Contains(str)))
                        message += str;
                    WarningPopup wp = new WarningPopup();
                    wp.Owner = this;
                    wp.TitleText = "Недостаточно данных";
                    wp.Message = message;
                    wp.ShowDialog();
                    e.Cancel = true;
                    return;
                }

                if (issueForm.IssueFormID == 0)
                    issueForm.Insert();
                else
                    issueForm.Update();
            }
            else
                issueForm.Cancel();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            new DTC.Resources.TextBoxBindingValidationSetter().PrepareBinding(MainGrid);
        }
    }
}
